package com.goods.business.service.imp;

import com.goods.business.mapper.ProductCategoryMapper;
import com.goods.business.service.BusinessService;
import com.goods.common.model.business.ProductCategory;
import com.goods.common.utils.CategoryTreeBuilder;
import com.goods.common.vo.business.ProductCategoryTreeNodeVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

@Service
public class BusinessServiceImpl implements BusinessService {

    @Autowired
    private ProductCategoryMapper productCategoryMapper;

    //查询全部
    @Override
    public List<ProductCategoryTreeNodeVO> categoryTree() {
        List<ProductCategoryTreeNodeVO> productCategoryTreeNodeVOList = getProductCategoryTreeNodeVOS();
        return CategoryTreeBuilder.build(productCategoryTreeNodeVOList);


    }

    //获取所有分类并转成ProductCategoryTreeNodeVO集合
    private List<ProductCategoryTreeNodeVO> getProductCategoryTreeNodeVOS() {
        //所有分类集合
        List<ProductCategory> ProductCategoryList = productCategoryMapper.selectAll();
        //创建我需要的分类集合
        List<ProductCategoryTreeNodeVO> productCategoryTreeNodeVOList = new ArrayList();
        ProductCategoryList.forEach(ProductCategory ->{
            ProductCategoryTreeNodeVO productCategoryTreeNodeVO = new ProductCategoryTreeNodeVO();
                productCategoryTreeNodeVO.setId(ProductCategory.getId());
                productCategoryTreeNodeVO.setName(ProductCategory.getName());
                productCategoryTreeNodeVO.setRemark(ProductCategory.getRemark());
                productCategoryTreeNodeVO.setCreateTime(ProductCategory.getCreateTime());
                productCategoryTreeNodeVO.setModifiedTime(ProductCategory.getModifiedTime());
                productCategoryTreeNodeVO.setSort(ProductCategory.getSort());
                productCategoryTreeNodeVO.setPid(ProductCategory.getPid());
                productCategoryTreeNodeVOList.add(productCategoryTreeNodeVO);
        });
        return productCategoryTreeNodeVOList;
    }

    //新增.添加
    @Override
    public boolean add(ProductCategory productCategory) {
        productCategory.setCreateTime(Calendar.getInstance().getTime());
        productCategory.setModifiedTime(Calendar.getInstance().getTime());
        int insert = productCategoryMapper.insert(productCategory);
        return insert==1;
    }


    //获取父类节点
    @Override
    public List<ProductCategoryTreeNodeVO> getParentCategoryTree() {
        List<ProductCategoryTreeNodeVO> productCategoryTreeNodeVOS = getProductCategoryTreeNodeVOS();
        return CategoryTreeBuilder.buildParent(productCategoryTreeNodeVOS);
    }

    //回显
    @Override
    public ProductCategory edit(Long id) {
        ProductCategory productCategory = new ProductCategory();
        productCategory.setId(id);
        return productCategoryMapper.selectOne(productCategory);
    }

    //修改
    @Override
    public void update(ProductCategory productCategory) {
        productCategory.setModifiedTime(Calendar.getInstance().getTime());
        productCategoryMapper.updateByPrimaryKeySelective(productCategory);
    }

    //删除
    @Override
    public Boolean delete(Long id) {
        ProductCategory productCategory = new ProductCategory();
        productCategory.setPid(id);
        List<ProductCategory> select = productCategoryMapper.select(productCategory);
        if(select != null &&select.size() >0){
            return false;
        }else{
            productCategoryMapper.deleteByPrimaryKey(id);
            return true;
        }
    }


    

}
